<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Fg extends Site {

    function setup() {
        $this->run_modes('show','list','hot','detail','clear');
        $this->start_mode('show');
        $this->selfurl = '/fg.php';
        parent::setup();
        include("common.php");
    }

    function cgiapp_prerun() {
        $this->Product = getModel('Model_Product');
        $this->Lookup = getModel('Model_Lookup');
        $this->FG = getModel('Model_FG');
        $this->indexBanner = getModel('Model_indexBanner');
        parent::cgiapp_prerun();
    }

    function action_show(){
		$kvcobj = getCache('fg_show', array('expire' => KVC_EXPIRE_fg));
		
		$left_data = $kvcobj->get('left_data');
        if (!$left_data) {
            $left_data = $this->FG->get_fg_hilite_left_product();
			if(!empty($left_data[0])){
				$left_data[0]['_img'] = PhotoResize($left_data[0]['imgurl'], 250, 250);
				//$left_data[0]['_icon'] = PhotoResize($left_data[0]['niceimg'], 99, 103);
				$left_data[0]['_title'] = wordscut($left_data[0]['type_name'].' - '.$left_data[0]['cname'], 20);
				$left_data[0]['_url'] = optimize_url('product', 'detail', 'mtype=' . $left_data[0]['member_type'] . '&tab=2&pid=' . $left_data[0]['pid']);
			}
            $kvcobj->set('left_data', $left_data);
        }
        
        $middle_data = $kvcobj->get('middle_data');
        if (!$middle_data) {
			$middle_data = $this->FG->get_fg_hilite_middle_product();
			if(!empty($middle_data)){
				$middle_data['product'][0]['_img'] = PhotoResize($middle_data['product'][0]['imgurl'], 100, 143);
				//$middle_data['product'][0]['_icon'] = PhotoResize($middle_data['product'][0]['niceimg'], 54, 56);
				$middle_data['product'][0]['_title'] = wordscut($middle_data['product'][0]['type_name'].' - '.$middle_data['product'][0]['cname'], 25);
				$middle_data['product'][0]['_url'] = optimize_url('product', 'detail', 'mtype=' . $middle_data['product'][0]['member_type'] . '&tab=2&pid=' . $middle_data['product'][0]['pid']);
				$middle_data['report'][0]['_url'] = optimize_url('product', 'fg', 'mtype=' . $middle_data['product'][0]['member_type'] . '&pid=' . $middle_data['product'][0]['pid'].'&rid='.$middle_data['report'][0]['id']);
				$middle_data['report'][0]['_msg'] = wordscut(strip_tags($middle_data['report'][0]['msg']),55);
				$img_array = array();
				preg_match_all('/<img.*?src=\"\/(.*?\.jpg)\".*?>/i', $middle_data['report'][0]['msg'], $img_array, PREG_PATTERN_ORDER);
				$img_array = array_unique($img_array[1]);
				if (!empty($img_array)) {
					foreach ($img_array as $k1 => $v1) {
						$middle_data['report'][0]['_pic'][$k1] = PhotoResize($v1, 130, 90,0);
						if ($k1 >= 1)
							break;
					}
				}
			}
		    $kvcobj->set('middle_data', $middle_data);
        }
		
		$h_param = array();
        $h_param['page'] = 1;
        $h_param['size'] = 16;
		$left_new_data = $kvcobj->get('left_new_data'.$h_param['page'].'_'.$h_param['size']);
        if (!$left_new_data) {
			$left_new_data = $this->FG->get_new_fg_report($h_param);
			if(!empty($left_new_data)){
				foreach($left_new_data as $k=>$v){
					$left_new_data[$k]['_img'] = PhotoResize($v['cover'], 78, 85);
					$left_new_data[$k]['_url'] = optimize_url('product', 'detail', 'mtype=' . $v['member_type'] . '&tab=2&pid=' . $v['id'] . '&tab=2');
				}
			}
		    $kvcobj->set('left_new_data'.$h_param['page'].'_'.$h_param['size'], $left_new_data);
        }

        $hilite_category = $kvcobj->get("hilite_category");
        if (!$hilite_category) {
            $hilite_category = $this->FG->get_fg_hilite_category();
            $kvcobj->set("hilite_category", $hilite_category);
        }
		
		$hilite_product_list = $kvcobj->get("hilite_product_list");
        if (!$hilite_product_list) {
			if(!empty($hilite_category)){
				foreach($hilite_category as $k=>$v){
					$hilite_product_list[$k] = $this->FG->get_fg_product_by_category(array('cid'=>$v['cid']));
                                        
					if(!empty($hilite_product_list[$k])){
						foreach($hilite_product_list[$k] as $kk=>$vv){
                            $hilite_product_list[$k][$kk]['cid'] = $v['cid'];                       
							$hilite_product_list[$k][$kk]['product'][0]['_img'] = PhotoResize($vv['product'][0]['imgurl'], 93, 100);
							//$middle_data['product'][0]['_icon'] = PhotoResize($middle_data['product'][0]['niceimg'], 54, 56);
							$hilite_product_list[$k][$kk]['product'][0]['_title'] = wordscut($vv['product'][0]['type_name'].' - '.$vv['product'][0]['cname'], 25);
							$hilite_product_list[$k][$kk]['product'][0]['_url'] = optimize_url('product', 'detail', 'mtype=' . $vv['product'][0]['member_type'] . '&tab=2&pid=' . $vv['product'][0]['pid']);
							$hilite_product_list[$k][$kk]['report'][0]['_url'] = optimize_url('product', 'fg', 'mtype=' . $vv['product'][0]['member_type'] . '&pid=' . $vv['product'][0]['pid'].'&rid='.$vv['report'][0]['id']);
							$hilite_product_list[$k][$kk]['report'][0]['_msg'] = wordscut(strip_tags($vv['report'][0]['msg']),35);
							$img_array = array();
							preg_match_all('/<img.*?src=\"\/(.*?\.jpg)\".*?>/i', $vv['report'][0]['msg'], $img_array, PREG_PATTERN_ORDER);
							$img_array = array_unique($img_array[1]);
							if (!empty($img_array)) {
								foreach ($img_array as $k1 => $v1) {
									$hilite_product_list[$k][$kk]['report'][0]['_pic'][$k1] = PhotoResize($v1, 68, 49,0);
									if ($k1 >= 1)
										break;
								}
							}
						}
					}
				}
			}
		$kvcobj->set("hilite_product_list", $hilite_product_list);
        }
		
		$hilite_product = array();
		if(!empty($hilite_product_list)){
			foreach($hilite_product_list as $k=>$v){
					$_c = count($v)-1;
					$_a=array(); 
					if($_c >= 3){
						while(count($_a)<3){ 
							$a = rand(0, $_c);
							if(!empty($v[$a]))
								$_a[$a]=null; //利用键的唯一性，确保不同的值
						}
						$_a=array_keys($_a);
						foreach($_a as $v1){
							$hilite_product[$k][] = $v[$v1];
						}
					}else{
						$hilite_product[$k] = $v;
					}
				}
		}

		//echo count($hilite_product[$k]);
		//echo '<pre>' . var_export($hilite_category, 1) . '</pre>';
		//echo '<pre>' . var_export($hilite_product_list[0][13], 1) . '</pre>';
		//echo '<pre>' . var_export($hilite_product_list[0], 1) . '</pre>';
		//echo '<pre>' . var_export($hilite_product[0], 1) . '</pre>';
		
		
        $this->tmpl_assign('left_data',$left_data);
		$this->tmpl_assign('left_new_data',$left_new_data);
        $this->tmpl_assign('middle_data',$middle_data);
		$this->tmpl_assign('hilite_category',$hilite_category);
		$this->tmpl_assign('hilite_product',$hilite_product);
        $this->tmpl_assign('pageoptions', array('csses'=>array('fg')));
        return $this->load_tmpl('fg_show.php');
    }
	
	function action_list(){
		$kvcobj = getCache('fg_list', array('expire' => KVC_EXPIRE_fg));
		
		$ptype = intval($this->get_q('ptype', 0, 'int'));
		
		//分類
        $product_layout_category_list = $kvcobj->get('product_layout_category_list');
        if (!$product_layout_category_list) {
            $product_layout_category_list = $this->Lookup->getLookup('product_layout_category', 0, 1, 9, 0);
            $kvcobj->set('product_layout_category_list', $product_layout_category_list);
        }
        //產品分類
        $ptype_arr = $kvcobj->get('ptype_arr');
        if (!$ptype_arr) {
            //$ptype_arr[0]['name'] = '所有分類';
            //$ptype_arr[0]['list'] = array();
            if (!empty($product_layout_category_list['result'])) {
                foreach ($product_layout_category_list['result'] as $k => $v) {
                    $ptype_li_list = $this->Lookup->getLookup('product_layout_category', 0, 1, 99, $v['id'], 0,0,1);
                    if (!empty($ptype_li_list['result'])) {
                        foreach ($ptype_li_list['result'] as $k1 => $v1) {
                            if ($v1['products_fg_num'] > 0) {
                                $ptype_arr[$v['id']]['list'][$k1] = $v1;
                            }
                        }
                    }
					$ptype_arr[$v['id']]['name'] = $v['cname'];
					/* if(!empty($ptype_arr[$v['id']]['list'])){
						$ptype_arr[$v['id']]['name'] = $v['cname'];
					} */
                }
            }
            $kvcobj->set('ptype_arr', $ptype_arr);
        }
		//echo '<pre>' . var_export($product_layout_category_list, 1) . '</pre>';
		//echo '<pre>' . var_export($ptype_arr, 1) . '</pre>';
		
        $ptype_kv = array();
        $ptype_kv['k'] = $ptype;
		$ptype_temp = $kvcobj->get('ptype_temp_'.$ptype);
        if (!$ptype_temp) {
			$ptype_temp = db_query_one('product_layout_category', array('field' => 'id,cname,topid', 'where' => 'id=' . $ptype));
		    $kvcobj->set('ptype_temp_'.$ptype, $ptype_temp);
        }
        $ptype_kv['v'] = !empty($ptype_temp['result']) ? $ptype_temp['result']['cname'] : '';
        $ptype_kv['t'] = !empty($ptype_temp['result']) ? $ptype_temp['result']['topid'] : '';
		
		$h_param = array();
        $h_param['cid'] = !empty($ptype_temp['result']['topid']) ? $ptype : 0;
        $h_param['big_cid'] = !empty($ptype_temp['result']['topid']) ? 0 : $ptype;
        $h_param['page'] = intval($this->get_q('page', 1, 'int'));
		$h_param['size'] = 10;
		
		$fg_list = $kvcobj->get('fg_list_'.$ptype.'_'.$h_param['page'].'_'.$h_param['size']);
        if (!$fg_list) {
			$fg_list = $this->FG->get_fg_product($h_param);
			if (!empty($fg_list['result'])) {
				foreach ($fg_list['result'] as $k => $v) {
				   $fg_list['result'][$k]['_img'] = PhotoResize($v['imgurl'], 100, 143);
					//$middle_data['product'][0]['_icon'] = PhotoResize($middle_data['product'][0]['niceimg'], 54, 56);
					$_name = empty($v['cname']) ? $v['name'] : $v['cname'];
					$fg_list['result'][$k]['_title'] = wordscut($v['type_name'].' - '.$_name, 30);
					$fg_list['result'][$k]['_url'] = optimize_url('product', 'detail', 'mtype=' . $v['member_type'] . '&tab=2&pid=' . $v['pid']);
				}
			}
		    $kvcobj->set('fg_list_'.$ptype.'_'.$h_param['page'].'_'.$h_param['size'], $fg_list);
        }
		//echo '<pre>' . var_export($fg_list, 1) . '</pre>';
		$pagelist = showPage($fg_list['pageinfo']['total'], $h_param['size'], $h_param['page'], optimize_url('fg', 'list', 'ptype='.$ptype));
        $this->tmpl_assign('ptype_arr', $ptype_arr);
        $this->tmpl_assign('ptype', $ptype);
        $this->tmpl_assign('ptype_kv', $ptype_kv);
        $this->tmpl_assign('fg_list', $fg_list);
        $this->tmpl_assign('pagelist', $pagelist);
		$this->tmpl_assign('pageoptions', array('csses'=>array('fg')));
        return $this->load_tmpl('fg_list.php');
	}
	
	function action_hot(){
		$kvcobj = getCache('fg_hot', array('expire' => KVC_EXPIRE_fg));
		$fg_blog_list = $kvcobj->get('fg_blog_list');
        if (!$fg_blog_list) {
			$h_param = $fg_blog_list = array();
			$h_param['id'] = 0;
			$h_param['page'] = 1;
			$h_param['size'] = 9;
			$fg_list = $this->FG->get_fg_blog_list($h_param);
			if(!empty($fg_list['result'])){
				foreach($fg_list['result'] as $k=>$v){
					$fg_blog_list[$k] = $v;
					$fg_blog_list[$k]['_title'] = wordscut(strip_tags($v['title']),20);
					$fg_blog_list[$k]['_content'] = wordscut(strip_tags($v['content']),38);
					$fg_blog_list[$k]['_photo'] = PhotoResize($v['user_avatar'], 60, 60);
					$fg_blog_list[$k]['_img'] = PhotoResize($v['mid_post_cover_url'], 210, 157);
					//if(count($fg_blog_list)>=9) break;
				}
			}
		    $kvcobj->set('fg_blog_list', $fg_blog_list);
        }
		
		//首頁flash數組
        $flash = $kvcobj->get('flash');
        if (!$flash) {
            $flash = $this->indexBanner->getindexBanner();
            $kvcobj->set('flash', $flash);
        }
		
        $this->tmpl_assign('flash', $flash);
        $this->tmpl_assign('fg_blog_list', $fg_blog_list);
		$this->tmpl_assign('pageoptions', array('csses'=>array('fg')));
        return $this->load_tmpl('fg_hot.php');
	}
	
	function action_detail(){
		$id = $this->get_q('id', 0, 'int');
		$kvcobj = getCache('fg_detail', array('expire' => KVC_EXPIRE_fg));
		$fg_list = $kvcobj->get('fg_list_'.$id);
        if (!$fg_list) {
			$h_param['id'] = $id;
			$h_param['page'] = 1;
			$h_param['size'] = 1;
			$fg_list = $this->FG->get_fg_blog_list($h_param);
			if(!empty($fg_list['result'])){
				foreach($fg_list['result'] as $k=>$v){
					$fg_list['result'][$k] = $v;
					$fg_list['result'][$k]['_photo'] = PhotoResize($v['user_avatar'], 60, 60);
					//if(count($fg_blog_list)>=9) break;
				}
			}
		    $kvcobj->set('fg_list_'.$id, $fg_list);
        }
		
        $this->tmpl_assign('fg_list', $fg_list);
		$pageoptions = array();
        $pageoptions['csses'][0] = "jquery.lightbox-0.5";
        $pageoptions['csses'][1] = "fg";
        $pageoptions['jses'][0] = "jquery.lightbox-0.5.min";
		$this->tmpl_assign('pageoptions',$pageoptions);
        return $this->load_tmpl('fg_detail.php');
		
	}
	
	function action_clear() {
        $kvc = $this->get_q('kvc', '');
		$kvcs = empty($kvc)? "fg_show" :$kvc;
        clear_kvcache($kvcs);
    }

}

$webapp = new Fg();
$webapp->run();
?>